package com.gt.quality.utils;

import lombok.Getter;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * @description:
 * @author: makeJava
 * @createTime: 2026-10-31 18:34
 * @since JDK21
 **/
public class RSAUtil {
    // RSA==公钥：支付宝
    public static String pubKey ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnELfK77L0IwkE+oJNqqLvNx6hwdUrjLshOKo7exMlafMoTJZxiF1PeEkhQMgFzP0qdRXejXs9VZDIirhR7/7+sMwKrrGd1808Z6AFElYMwNercF1rJkcmOrJRSFtwn5J7gZd5fTT2r4KVE21z7HOYP4V7VyUmYlkSlUWGe2IfZE/fB8QwUQQRoXGfgB93deuwI09sVswNyyYuJVxrXf8yyB9yTAWWkLgzFvKnChXsRmiYRNdy1t/xJpiGVW/ZVC8dVMjcnCdDm7qHurztDkcjYfhdMGl8fLDkSMulqNF1WfPlxBlos5i1jGLA37lhPgskOiI470I2syJdFTKe6sn2QIDAQAB";

    // RSA==私钥：支付宝
    public static String priKey= "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCcQt8rvsvQjCQT6gk2qou83HqHB1SuMuyE4qjt7EyVp8yhMlnGIXU94SSFAyAXM/Sp1Fd6Nez1VkMiKuFHv/v6wzAqusZ3XzTxnoAUSVgzA16twXWsmRyY6slFIW3CfknuBl3l9NPavgpUTbXPsc5g/hXtXJSZiWRKVRYZ7Yh9kT98HxDBRBBGhcZ+AH3d167AjT2xWzA3LJi4lXGtd/zLIH3JMBZaQuDMW8qcKFexGaJhE13LW3/EmmIZVb9lULx1UyNycJ0Obuoe6vO0ORyNh+F0waXx8sORIy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yz9QQ2yvOwCFKIA+m34JLVDO2rpf5LtlKmHXhfZFnwCRk6k+ZGzdkDPBq7QJa8fo+m0SSrW3k7jIaYXiZiDKRyVxxc8v6CgMoy8bypt3fQraVl3z0KjYFWx0jZuKsQDMaURwJiy6Hap8WARhlpzBevoXdsP9KXuJzpUviJGriDU=";
    /** 私钥解密
     *
     * @param privateKeyText privateKeyText
     * @param text  text
     * @return String
     * @throws Exception Exception
     */
    public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyText));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] result = cipher.doFinal(Base64.getDecoder().decode(text));
        return new String(result);
    }
    /**
     * 公钥加密
     *
     * @param publicKeyText publicKeyText
     * @param text text
     * @return String
     */
    public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyText));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] result = cipher.doFinal(text.getBytes());
        return Base64.getEncoder().encodeToString(result);
    }

    /**
     * 构建RSA密钥对
     *
     * @return RSAKeyPair
     * @throws NoSuchAlgorithmException NoSuchAlgorithmException
     */
    public static RSAKeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        String publicKeyString = Base64.getEncoder().encodeToString(rsaPublicKey.getEncoded());
        String privateKeyString = Base64.getEncoder().encodeToString(rsaPrivateKey.getEncoded());
        return new RSAKeyPair(publicKeyString, privateKeyString);
    }


    /**
     * RSA密钥对对象
     */
    @Getter
    public static class RSAKeyPair {

        private final String publicKey;
        private final String privateKey;

        public RSAKeyPair(String publicKey, String privateKey) {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }

    }
    /**
     * 公钥加密私钥解密
     */
    private static void test1(RSAKeyPair keyPair, String source) throws Exception {
        System.out.println("***************** 公钥加密私钥解密开始 *****************");
        String text1 = encryptByPublicKey(keyPair.getPublicKey(), source);
        String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1);
        System.out.println("加密前：" + source);
        System.out.println("加密后：" + text1);
        System.out.println("解密后：" + text2);
        if (source.equals(text2)) {
            System.out.println("解密字符串和原始字符串一致，解密成功");
        } else {
            System.out.println("解密字符串和原始字符串不一致，解密失败");
        }
        System.out.println("***************** 公钥加密私钥解密结束 *****************");
    }

    public static void main(String[] args) {
        Runtime.Version version = Runtime.version();
        System.out.println("Java Version: " + version);
        RSAKeyPair rsaKeyPair = new RSAKeyPair(pubKey, priKey);
        try {
            test1(rsaKeyPair,"admin");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
